import { store } from '@/store'
import { defineStore } from 'pinia'
import { setToken as _setToken, getToken, removeToken } from '@/utils/cache/cookies'
import { ref } from 'vue'
import { randomUUID } from 'vis/lib/util'

export const useUserStore = defineStore('user', () => {
  // 响应式 token
  const token = ref(getToken() || '')
  const userId = ref(randomUUID())

  // 设置 token
  const setToken = (value) => {
    _setToken(value)
    token.value = value
  }

  // 登出
  const logout = () => {
    removeToken()
    token.value = ''
  }

  // 可选：重置 token
  const resetToken = () => {
    removeToken()
    token.value = ''
  }

  // 导出
  return {
    token,
    userId,
    setToken,
    logout,
    resetToken
  }
})

export function useUserStoreOutside() {
  return useUserStore(store)
}
